package net.aiion.weave.impl.naming;

import net.aiion.weave.spi.naming.Context;
import net.aiion.weave.spi.naming.Contextual;
import net.aiion.weave.spi.naming.ConversationContext;
import net.aiion.weave.spi.naming.SessionContext;
import net.aiion.weave.spi.naming.annotations.SessionScoped;

public class SessionContextImpl extends ContextImpl implements SessionContext
{
    private static final String SESSION_CONTEXT_NAME = SessionContext.class.getName();

    public SessionContextImpl(final Context parentContext) {
        super(SessionScoped.class);
        setParentContext(parentContext);
    }

    @Override
    public ConversationContext createConversationContext() {
        return new ConversationContextImpl(this);
    }
    
    @Override
    public ConversationContext lookupConversationContext(final String aConversationContextId) {
        final Context context = getChildContextById(aConversationContextId);
        if (!(context instanceof ConversationContext)) {
            return createConversationContext();
        }

        return (ConversationContext) context;
    }

    @Override
    protected Contextual lookupContext(final String bindingName) {
        if (SESSION_CONTEXT_NAME.equals(bindingName)) {
            return bind(bindingName, this);
        }

        return super.lookupContext(bindingName);
    }
}
